package com.zdf.license.core.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.stream.Stream;

/**
 * <p>用于获取客户Linux服务器的基本信息</p>
 */
public class LinuxServerInfos extends AServerInfos {

    @Override
    protected String getCPUSerial() throws IOException {
        return exec("dmidecode -t 4 | grep ID |sort -u |awk -F': ' '{print $2}' | tail -1");

    }

    @Override
    protected String getMainBoardSerial() throws Exception {
        return exec("dmidecode -t system | grep 'Serial Number' | awk '{print $3}'");
    }

    private String exec(String cmd) throws IOException {
        StringBuilder result= new StringBuilder();
        Process p = Runtime.getRuntime().exec(new String[]{ "sh", "-c", cmd});// 管道
        try (
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            Stream<String> stringStream = br.lines()
        ) {
            stringStream.forEach(result::append);
        }
        return  result.toString().trim();
    }

}

